#include "stdafx.h"

Player::Player(void)
{
    // Create Sprite 
    mSprite = gcnew Sprite(ResourceManager::GetTexture("Player"));

    // Set Size
    Size = Coordinates::Size(32, 64);

    // Set Collision Offset
    CollisionOffset = Coordinates::Rectangle(0, 32, 32, 29);
}


String^ Player::Name::get(void)
{
    return mName;
}


int Player::Health::get(void)
{
    return mHealth;
}


void Player::Health::set(int value)
{
    mHealth = value;
}


int Player::Agility::get(void)
{
    return mAgility;
}


void Player::Agility::set(int value)
{
    mAgility = value;
}


int Player::Strength::get(void)
{
    return mStrength;
}


void Player::Strength::set(int value)
{
    mStrength = value;
}


int Player::Intelligence::get(void)
{
    return mIntelligence;
}


void Player::Intelligence::set(int value)
{
    mIntelligence = value;
}


int Player::Stamina::get(void)
{
    return mStamina;
}


void Player::Stamina::set(int value)
{
    mStamina = value;
}


List<Item^>^ Player::Inventory::get(void)
{
    return %mInventory;
}


void Player::Update(void)
{
    // Get World
    World^ world = Core::Engine::GetWorld();

    // Get Velocity At Current Tile
    Tile^ tile = world->Layers["Background"]->Map->GetSquare((int)(CollisionRectangle.X / 32.0f), (int)(CollisionRectangle.Y / 32.0f));
    int maxVelocity = tile->Velocity;
    if (maxVelocity == 0)
        maxVelocity = 5;
    int offset = 0;
    Coordinates::Location velocity;
    do
    {
        if (Actions.MoveLeft)
        {
            velocity.X = (float)(-1 * maxVelocity + offset);
        }
        else if (Actions.MoveRight)
        {
            velocity.X = (float)(maxVelocity - offset);
        }

        if (Actions.MoveUp)
        {
            velocity.Y = (float)(-1 * maxVelocity + offset);
        }
        else if (Actions.MoveDown)
        {
            velocity.Y = (float)(maxVelocity - offset);
        }

        // Try Moving in X and Y
        if (Move(velocity))
            break;

        // Try Moveing in Just X
        if (Actions.MoveLeft || Actions.MoveRight)
        {
            if (Move(Coordinates::Location(velocity.X, 0)))
                break;
        }

        // Try Moving in Just Y
        if (Actions.MoveDown || Actions.MoveUp)
        {
            if (Move(Coordinates::Location(0, velocity.Y)))
                break;
        }

        // Increment offset
        offset++;
    } while( (offset < maxVelocity) );

    // Update Player Sprite
    {
        mSprite->X = Location.X;
        mSprite->Y = Location.Y;
        mSprite->Width = Size.Width;
        mSprite->Height = Size.Height;
    }

    // Update Collision
    {
        UpdateCollision();
    }
}


void Player::GetSprites(List<Sprite^>^ argSpriteList)
{
    // Add Sprite to Sprite List
    argSpriteList->Add(mSprite);
}

